package com.tian.hello.ui.topline;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.tian.hello.ui.topline.base.IViewModel;
import com.tian.hello.ui.topline.base.ItemInfo;
import com.tian.hello.ui.topline.base.ViewHolder;
import com.tian.hello.ui.topline.model.FriendViewModel;
import com.tian.hello.ui.topline.model.ImageModel;
import com.tian.hello.ui.topline.model.TxtModel;
import com.tian.hello.ui.topline.model.VedioModel;

/**
 * 多item布局的列表 适配器，通过添加{@link IViewModel}的实现类,实现对多item布局的支持。
 * <pre>
 * //简便模式
 * itemInfos.add(new ItemInfo(IViewModel.MODEL_VEDIO));
 * itemInfos.add(new ItemInfo(IViewModel.MODEL_TXT));
 * itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
 * adapter = new ComplexListAdapter(this, itemInfos);
 * -----------------
 * //演示动态添加
 * itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
 * adapter.addModel(new ImageModel());
 * itemInfos.add(new ItemInfo(IViewModel.MODEL_VEDIO));
 * adapter.addModel(new VedioModel());
 * adapter.notifyDataSetChanged();
 * <pre/>
 *
 * <p>@author：skyTian
 * <br>@time:2018/2/1
 * <br>@copyright:com.sky
 */
public class ComplexListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ItemInfo> mDatas;

    /**
     * ViewModel处理对象
     */
    private ArrayList<IViewModel<ItemInfo>> models;

    /**
     * 动态添加当前列表支持的ViewModel,配合3个参数构造方法使用，添加一种数据类型就应该添加一个IViewmodel的实现类，页面上调用此方法设置给adapter
     *
     * @param model
     */
    public void addModel(IViewModel<ItemInfo> model) {
        this.models.add(model);
    }

    /**
     * 清空支持的 viewmodel类型
     */
    public void clearModels() {
        this.models.clear();
    }


    /**
     * 构造方法
     *
     * @param mContext
     * @param mDatas
     * @param models
     */
    public ComplexListAdapter(Context mContext, List<ItemInfo> mDatas, ArrayList<IViewModel<ItemInfo>> models) {
        this.mContext = mContext;
        this.mDatas = (mDatas == null ? new ArrayList<ItemInfo>() : mDatas);
        this.models = (models == null ? new ArrayList<IViewModel<ItemInfo>>() : models);
    }

    /**
     * 简便模式,在adapter中，直接初始化可能含有的ViewMode,如果使用简便模式，在添加一个view类型的时候请
     * 如方法中一样手动add进models中，否则会以默认的文本布局进行显示.
     *
     * @param mContext
     * @param mDatas
     */
    public ComplexListAdapter(Context mContext, List<ItemInfo> mDatas) {
        this.mContext = mContext;
        this.mDatas = (mDatas == null ? new ArrayList<ItemInfo>() : mDatas);
        this.models = new ArrayList<>();

        models.add(new TxtModel());
        models.add(new ImageModel());
        models.add(new VedioModel());
        models.add(new FriendViewModel());
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = ViewHolder.create(mContext, getRecyclerViewModel(viewType).getItemLayout(), parent);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        IViewModel recyclerViewModel = getRecyclerViewModel(getItem(position).getViewType());
        if (recyclerViewModel != null)
            recyclerViewModel.onBindView(viewHolder, mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getViewType();
    }


    public ItemInfo getItem(int position) {
        if (mDatas.isEmpty()) {
            return null;
        }
        return mDatas.get(position);
    }

    /**
     * viewMode对象获取
     *
     * @param viewType
     * @return
     */
    private IViewModel getRecyclerViewModel(int viewType) {
        for (IViewModel recyclerViewModel : models) {
            if (recyclerViewModel.getViewType() == viewType)
                return recyclerViewModel;
        }
        Toast.makeText(mContext, "含有未定义ViewModel的类型，请添加对于数据类型的ViewMode类，如没有对于的ViewModel默认以文本数据类型显示", Toast.LENGTH_LONG).show();
        return new TxtModel();
    }
}
